在 Day 03 - Linepay (2) 功能介紹 有提到,一般付款有 授權 和 請款 兩個步驟,今天的內容會介紹到 LINE Pay 一次付清的功能。
由於 LINE Pay 不允許賣家直接從使用者瀏覽頁面發送 API ,所以我們把顯示商品資訊留在 index.php,將 reserve 和 confirm 的動作分割到不同檔案。

▲ 當買家點選 一次付清 的動作後,商品資訊會 POST 到 reserve.php
  $header = array(
    'Content-Type: application/json; charset=UTF-8'
    , 'X-LINE-ChannelId: 1234567890'
    , 'X-LINE-ChannelSecret: iamrutenbackendeddie'
  );
▲ 首先,先把 ChannelId 和 ChannelSecret 放到 header 中
  $postData = array(
    'productName' => (sizeof($_POST['productName'])==0) ? "Test Item" : $_POST['productName']
    , 'productImageUrl' => $_POST['productImageUrl']
    , 'amount' => 168
    , 'currency' => "TWD"
    , 'confirmUrl' => "http://eddie27.byethost11.com/linepay/confirm.php"
    , 'orderId' => $_POST['orderId']
  );
▲ 再來將必要欄位都填上,
| 項目 | 資料型別 | 是否必要? | 說明 | 
|---|---|---|---|
| productName | String | Y | 產品名稱 (charset:"UTF-8") | 
| productImageUrl | String | N | 產品影像 URL (或使用品牌 logo URL) | 
| amount | number | Y | 付款金額 | 
| currency | String | Y | 付款貨幣 (ISO 4217: USD, JPY, TWD, THB) | 
| confirmUrl | String | Y | 買家在 LINE Pay 選擇付款方式並輸入密碼後,被重新導向到商家的 URL。 | 
| orderId | String | Y | 商家與該筆付款請求對應的訂單編號(是商家自行管理的唯一編號) | 
$ch = curl_init("https://sandbox-api-pay.line.me/v2/payments/request");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//*curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
▲ 接下來就是流程圖中的 Step.2,發送 request 給 LINE Pay Server,請他回傳付款頁面的連結。
{
  "returnCode": "0000",
  "returnMessage": "OK",
  "info": {
    "transactionId": 123123123123,
    "paymentUrl": {
      "web": "http://web-pay.line.me/web/wait?transactionReserveId=blahblah",
      "app": "line://pay/payment/blahblah"
    },
    "paymentAccessToken": "187568751124"
  }
}
▲ 接著來到了 Step.3,直接導到 Server 回傳的 info.paymentUrl.web 網址
▲ 這邊可以選擇使用網頁版,或是手機掃 QR Code 的方式登入。
▲ 登入成功後會跳出付款視窗 (需允許瀏覽器彈出視窗)。
▲ Step.6 付款完成後原頁面會被導到 confirmUrl (confirm.php) 。
                                              
▲ 若手機有開啟付款通知也會顯示。
這樣我們就完成一次付清支付 API 的串接了。
測試網址
想請問一下 付款頁面的連結 若設定為 $result['info']['paymentUrl']['app'] 於app上開啟連結後都是顯示載入失敗是那邊沒有設定好嗎?
我也正想問這個
使用['app']
竟然會失敗
官方回覆為:
paymentUrl.app在Sandbox試環境中是無法測試的,
建議可以使用paymentUrl.web或者切換至Production正式環境測試paymentUrl.app。
有點坑的API文檔!